m_AUSUnitCommands = {}

local AUS_EXPLORER_INDEX = GameInfo.Units['UNIT_AUS_EXPLORER'].Index
local AUS_RESOURCE_FLAG = "AUS_RESOURCE_"
local AUS_GOLD_RUSH_FLAG = "AUS_GOLD_RUSH"
local AUS_BREED_IMPORT_FLAG = "AUS_BREED_IMPORT"
local AUS_LONG_RANG_FISHING_FLAG = "AUS_LONG_RANG_FISHING"

local COAST_INDEX = GameInfo.Terrains['TERRAIN_COAST'].Index;
local OCEAN_INDEX = GameInfo.Terrains['TERRAIN_OCEAN'].Index;
-- ================================================================================
-- 淘金热
-- ================================================================================
m_AUSUnitCommands.GOLD_RUSH = {};
m_AUSUnitCommands.GOLD_RUSH.Properties = {};
m_AUSUnitCommands.GOLD_RUSH.EventName = "AUSGoldRush";
m_AUSUnitCommands.GOLD_RUSH.CategoryInUI = "SPECIFIC";
m_AUSUnitCommands.GOLD_RUSH.Icon = "ICON_UNITCOMMAND_GOLD_RUSH";
m_AUSUnitCommands.GOLD_RUSH.ToolTipString = Locale.Lookup("LOC_UNITCOMMAND_GOLD_RUSH_NAME") .. "[NEWLINE][NEWLINE]" .. Locale.Lookup("LOC_UNITCOMMAND_GOLD_RUSH_DESCRIPTION");
m_AUSUnitCommands.GOLD_RUSH.VisibleInUI = true;
m_AUSUnitCommands.GOLD_RUSH.DoNotDelete = true;

function m_AUSUnitCommands.GOLD_RUSH.CanUse(unit: object)
	if unit == nil then
		return false;
	end
	return unit:GetType() == AUS_EXPLORER_INDEX;
end

function m_AUSUnitCommands.GOLD_RUSH.IsVisible(unit: object)
  if not unit then
    return false;
  end

  if unit:GetProperty(AUS_GOLD_RUSH_FLAG) == 1 then
    return false;
  end

  if unit:GetMovesRemaining() == 0 then
		return false;
	end

  local plotId = unit:GetPlotId();
	local plot = Map.GetPlotByIndex(plotId);
  if plot:GetOwner() ~= -1 then
    return false;
  end

  if plot:GetTerrainType() == COAST_INDEX or plot:GetTerrainType() == OCEAN_INDEX then
    return false;
  end

  local resourceId = plot:GetResourceType()
  if resourceId == -1 then
    return false;
  end

  local ownerId = unit:GetOwner();
	local player = Players[ownerId];
  if player:GetProperty(AUS_RESOURCE_FLAG .. resourceId) == 1 then
    return false;
  end

  local resourceHash = plot:GetResourceTypeHash()
  if not player:GetResources():IsResourceVisible(resourceHash) then
    return false;
  end

  local resourceType = GameInfo.Resources[resourceId].ResourceType
  for row in GameInfo.Improvement_ValidResources() do
    if row.ResourceType == resourceType and (row.ImprovementType == 'IMPROVEMENT_MINE' or row.ImprovementType == 'IMPROVEMENT_QUARRY') then
      return true;
    end
  end

  return false;
end

-- ================================================================================
-- 畜种引进
-- ================================================================================
m_AUSUnitCommands.BREED_IMPORT = {};
m_AUSUnitCommands.BREED_IMPORT.Properties = {};
m_AUSUnitCommands.BREED_IMPORT.EventName = "AUSBreedImport";
m_AUSUnitCommands.BREED_IMPORT.CategoryInUI = "SPECIFIC";
m_AUSUnitCommands.BREED_IMPORT.Icon = "ICON_UNITCOMMAND_BREED_IMPORT";
m_AUSUnitCommands.BREED_IMPORT.ToolTipString = Locale.Lookup("LOC_UNITCOMMAND_BREED_IMPORT_NAME") .. "[NEWLINE][NEWLINE]" .. Locale.Lookup("LOC_UNITCOMMAND_BREED_IMPORT_DESCRIPTION");
m_AUSUnitCommands.BREED_IMPORT.VisibleInUI = true;
m_AUSUnitCommands.BREED_IMPORT.DoNotDelete = true;

function m_AUSUnitCommands.BREED_IMPORT.CanUse(unit: object)
	if unit == nil then
		return false;
	end
	return unit:GetType() == AUS_EXPLORER_INDEX;
end

function m_AUSUnitCommands.BREED_IMPORT.IsVisible(unit: object)
  if not unit then
    return false;
  end

  if unit:GetProperty(AUS_BREED_IMPORT_FLAG) == 1 then
    return false;
  end

  if unit:GetMovesRemaining() == 0 then
		return false;
	end

  local plotId = unit:GetPlotId();
	local plot = Map.GetPlotByIndex(plotId);
  if plot:GetOwner() ~= -1 then
    return false;
  end

  if plot:GetTerrainType() == COAST_INDEX or plot:GetTerrainType() == OCEAN_INDEX then
    return false;
  end

  local resourceId = plot:GetResourceType()
  if resourceId == -1 then
    return false;
  end

  local ownerId = unit:GetOwner();
	local player = Players[ownerId];
  if player:GetProperty(AUS_RESOURCE_FLAG .. resourceId) == 1 then
    return false;
  end

  local resourceHash = plot:GetResourceTypeHash()
  if not player:GetResources():IsResourceVisible(resourceHash) then
    return false;
  end

  local resourceType = GameInfo.Resources[resourceId].ResourceType
  for row in GameInfo.Improvement_ValidResources() do
    if row.ResourceType == resourceType and (row.ImprovementType == 'IMPROVEMENT_PASTURE' or row.ImprovementType == 'IMPROVEMENT_CAMP') then
      return true;
    end
  end

  return false;
end

-- ================================================================================
-- 远洋捕捞
-- ================================================================================
m_AUSUnitCommands.LONG_RANG_FISHING = {};
m_AUSUnitCommands.LONG_RANG_FISHING.Properties = {};
m_AUSUnitCommands.LONG_RANG_FISHING.EventName = "AUSLongRangFishing";
m_AUSUnitCommands.LONG_RANG_FISHING.CategoryInUI = "SPECIFIC";
m_AUSUnitCommands.LONG_RANG_FISHING.Icon = "ICON_UNITCOMMAND_LONG_RANG_FISHING";
m_AUSUnitCommands.LONG_RANG_FISHING.ToolTipString = Locale.Lookup("LOC_UNITCOMMAND_LONG_RANG_FISHING_NAME") .. "[NEWLINE][NEWLINE]" .. Locale.Lookup("LOC_UNITCOMMAND_LONG_RANG_FISHING_DESCRIPTION");
m_AUSUnitCommands.LONG_RANG_FISHING.VisibleInUI = true;
m_AUSUnitCommands.LONG_RANG_FISHING.DoNotDelete = true;

function m_AUSUnitCommands.LONG_RANG_FISHING.CanUse(unit: object)
	if unit == nil then
		return false;
	end
	return unit:GetType() == AUS_EXPLORER_INDEX;
end

function m_AUSUnitCommands.LONG_RANG_FISHING.IsVisible(unit: object)
  if not unit then
    return false;
  end

  if unit:GetProperty(AUS_LONG_RANG_FISHING_FLAG) == 1 then
    return false;
  end

  if unit:GetMovesRemaining() == 0 then
		return false;
	end

  local plotId = unit:GetPlotId();
	local plot = Map.GetPlotByIndex(plotId);
  if plot:GetOwner() ~= -1 then
    return false;
  end

  if plot:GetTerrainType() ~= COAST_INDEX and plot:GetTerrainType() ~= OCEAN_INDEX then
    return false;
  end

  local resourceId = plot:GetResourceType()
  if resourceId == -1 then
    return false;
  end

  local ownerId = unit:GetOwner();
	local player = Players[ownerId];
  if player:GetProperty(AUS_RESOURCE_FLAG .. resourceId) == 1 then
    return false;
  end

  local resourceHash = plot:GetResourceTypeHash()
  if not player:GetResources():IsResourceVisible(resourceHash) then
    return false;
  end

  local resourceType = GameInfo.Resources[resourceId].ResourceType
  for row in GameInfo.Improvement_ValidResources() do
    if row.ResourceType == resourceType and row.ImprovementType == 'IMPROVEMENT_FISHING_BOATS' then
      return true;
    end
  end

  return false;
end